#include <mutex>

/*
饿汉模式: 类加载时初始化实例，线程安全。
*/
class singleton_hungury{
private:
    singleton_hungury() {}
    static singleton_hungury* singleton;

public:
    static singleton_hungury* GetInstance()
    {
        return singleton;
    }

    void showInfo()
    {
        printf("This is singleton hungry. singleton:0x%08X\n", singleton);
    }
};

singleton_hungury* singleton_hungury::singleton = new singleton_hungury();


/*
懒汉模式: 首次使用时初始化实例象，非线程安全。
*/
class singleton_lazy{
private:
    static singleton_lazy* m_singleton;
    static std::mutex m_mutex;

public:
    static singleton_lazy* Getinstance()
    {
        if(m_singleton == nullptr)
        {
            std::lock_guard<std::mutex> lock(m_mutex);
            if(m_singleton == nullptr)
            {
                m_singleton = new singleton_lazy();
                printf("Create instance first. singleton:0x%08X\n", m_singleton);
            }
        }

        return m_singleton;
    }

    void showInfo()
    {
        printf("This is singleton lazy.singleton:0x%08X\n", m_singleton);
    }
};

singleton_lazy* singleton_lazy::m_singleton = nullptr;
std::mutex singleton_lazy::m_mutex;